package com.huaxin.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.huaxin.common.R;


/**
 * Created by Administrator on 2016/10/28.
 */

public class StaticSearchBar extends Toolbar implements View.OnClickListener {


    private final static int STATE_CANCEL = 0x00;
    private final static int STATE_SEARCH = 0x01;

    EditText editView;
    ImageView delete;
    TextView search;
    private String keyword;
    private int state = STATE_CANCEL;
    private SearchListener mListener;
    Context mContext;
    private String lastKeyword;

    public StaticSearchBar(Context context) {
        super(context);
        mContext =context;
    }

    public StaticSearchBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public StaticSearchBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        delete = (ImageView) findViewById(R.id.delete);
        search = (TextView) findViewById(R.id.search);
        editView = (EditText) findViewById(R.id.editView);
        delete.setVisibility(View.INVISIBLE);
        delete.setOnClickListener(this);
        search.setText("取消");
        search.setOnClickListener(this);
        editView.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    delete.setVisibility(View.VISIBLE);
                    editView.setImeOptions(s.length() > 0 ? EditorInfo.IME_ACTION_SEARCH : EditorInfo.IME_NULL);
                    search.setText("搜索");
                    state = STATE_SEARCH;
                } else {
                    delete.setVisibility(View.INVISIBLE);
                    search.setText("取消");
                    state = STATE_CANCEL;
                }
            }
        });
        editView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keyword = editView.getText().toString().trim();
                    if(mListener!=null){
                        mListener.doSearch(keyword);
                    }
                } else {
                    hideIme();
                }
                return true;
            }
        });
    }

    public void hideIme() {
        InputMethodManager ime = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if(ime.isActive(editView)){
            ime.hideSoftInputFromWindow(editView.getWindowToken(),0);
        }

    }

    public void setListener(SearchListener listener){
        mListener = listener;
    }




    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.delete) {
            editView.getEditableText().clear();

        } else if (i == R.id.search) {
            if (state == STATE_SEARCH) {
                search.setText("取消");
                state = STATE_CANCEL;
                keyword = editView.getEditableText().toString().trim();
                if (mListener != null&&keyword!=lastKeyword) {
                    mListener.doSearch(keyword);
                }
                lastKeyword = keyword;
            } else {
                hideIme();
                if (mListener != null) {
                    mListener.cancel();
                }
            }
        }
    }

    public interface SearchListener{
        void doSearch(String keyword);
        void cancel();
    }


}
